Fix adjustFormat dropping d3-format specs that start with a sign flag - #7900
Fix adjustFormat dropping d3-format specs that start with a sign flag#7900TemRevil wants to merge 5 commits into
Conversation
A d3-format specifier beginning with a sign flag (+, -, (, space) was
silently ignored: adjustFormat prepended a trim tilde to the whole
string, producing an invalid spec like ~+.2f. d3.format then threw,
numberFormat fell back to noFormat, and the raw unformatted number was
shown (e.g. %{y:+.2f} rendered 12.3456789 instead of +12.35).
Skip past an optional leading sign flag and symbol ($, #) before the
trim heuristic and reattach it, so the tilde is only added in a valid
position. Formats without such a prefix are unaffected.
Fixes plotly#7897
|
Thanks for the PR! I'll take a look and follow up. |
camdecoster
left a comment
There was a problem hiding this comment.
Thanks for the fix! I'd like to make some changes to avoid some unintended consequences of this update.
| // symbol ($, #). Look past that prefix before deciding whether to trim, and | ||
| // reattach it: prepending the tilde to the whole string (e.g. "~+.2f") is an | ||
| // invalid spec that d3Format rejects, so "+.2f" used to be silently dropped. | ||
| var prefix = (formatStr.match(/^[+\-( ]?[$#]?/) || [''])[0]; |
There was a problem hiding this comment.
Let's remove these from the prefix group. Without removing these, "$f" will get changed to "$~f". This means that "1.50" would get changed to "$1.5", which isn't what we want.
| var prefix = (formatStr.match(/^[+\-( ]?[$#]?/) || [''])[0]; | |
| var prefix = (formatStr.match(/^[+\-( ]/) || [''])[0]; |
| var rest = formatStr.slice(prefix.length); | ||
|
|
||
| // try adding tilde to trim trailing zeros | ||
| if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest; |
There was a problem hiding this comment.
| if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest; | |
| if (!/^[~,.0$#]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest; |
| // sign flag with an explicit precision must not be dropped (was silently | ||
| // ignored because adjustFormat produced the invalid spec "~+.2f") | ||
| { format: '+.2f', number: float, exp: '+12345.68'}, | ||
| { format: '+.0f', number: float, exp: '+12346'}, | ||
| { format: '-.4f', number: float, exp: '12345.6789'}, |
There was a problem hiding this comment.
| // sign flag with an explicit precision must not be dropped (was silently | |
| // ignored because adjustFormat produced the invalid spec "~+.2f") | |
| { format: '+.2f', number: float, exp: '+12345.68'}, | |
| { format: '+.0f', number: float, exp: '+12346'}, | |
| { format: '-.4f', number: float, exp: '12345.6789'}, | |
| // sign flag with an explicit precision must not be dropped | |
| { format: '+.2f', number: float, exp: '+12345.68'}, | |
| { format: '+.0f', number: float, exp: '+12346'}, | |
| { format: '-.4f', number: float, exp: '12345. | |
| // symbol-led specs ($, #) are left untrimmed | |
| { format: '$f', number: 1.5, exp: '$1.500000'}, | |
| { format: '#f', number: 1.5, exp: '1.500000'}, | |
| { format: '$s', number: 1500, exp: '$1.50000k'}, |
| @@ -0,0 +1 @@ | |||
| - Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` silently ignoring d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)] | |||
There was a problem hiding this comment.
| - Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` silently ignoring d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)] | |
| - Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` improperly handling d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)] |
|
i'm getting some mind melting currently, so i'll review it and send you back. |
camdecoster pointed out that folding $/# into the prefix group broke symbol-led specs: "$f" became "$~f", an invalid spec, and stripping the symbol before the trim check meant "$1.500000" would come out as "$1.5" once the tilde got added. Only sign flags (+, -, (, space) belong in the stripped prefix; symbol-led specs now fall straight through to the untrimmed return, matching the existing hex/octal/binary "#" cases. Added test coverage for $f/#f/$s, reworded the draftlog line to "improperly handling" since it's not strictly a silent drop anymore.
|
alright, back on this, sorry for the wait. applied all four: narrowed the prefix match to just the sign flags, added # to the rest-exclusion regex, reworded the draftlog line, and fixed up the test file. one thing on that last part, the suggested diff cut off mid-string ( ran those plus the rest of the suite against d3-format directly, all correct. thanks for digging into this, the $/# distinction wasn't something I'd thought through carefully enough the first time. |
Fixes #7897
A d3-format specifier that begins with a sign flag (
+,-,(, or a space) was silently ignored inhovertemplate,texttemplate,tickformat, andhoverformat, showing the raw unformatted number instead. For example%{y:+.2f}rendered12.3456789rather than+12.35.Cause:
Lib.adjustFormatprepends a trim tilde to the start of the whole string, so+.2fbecame~+.2f— an invalid spec (the~flag must sit just before the type).d3.formatthrew,Lib.numberFormatcaught it and fell back toLib.noFormat.Fix: skip past an optional leading sign flag and symbol (
$,#) before the trim heuristic, then reattach that prefix, so the tilde is only inserted in a valid position. Formats without such a prefix are unchanged.Verified against
d3-formatv1 directly:+.2f→+12345.68,+.0f→+12346,-.4f→12345.6789, while.2f,s,0.3s,+13,-13,($15,#0X,.2%produce identical output to before. Added matching cases intest/jasmine/tests/lib_number_format_test.js.